package com.bsg.upm.aspect;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.bsg.upm.constant.DictConstants;
import com.bsg.upm.entity.UserEntity;

/**
 * 角色切面类
 * 
 * @author HCK
 *
 */
@Aspect
@Component
public class RoleAspect {

	protected Logger logger = Logger.getLogger(getClass());

	@Pointcut("execution(public * com.bsg.upm.service.*.list(..)) ")
	public void pointcut() {

	}

	@SuppressWarnings("unchecked")
	@Around("pointcut()")
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		HttpSession session = request.getSession();
		UserEntity user = (UserEntity) session.getAttribute("user");
		Object[] args = pjp.getArgs();
		if (args != null) {
			for (Object arg : args) {
				if (arg instanceof Map) {
					if (DictConstants.DATA_SCOPE_ONESELF.equals(user.getUserRole().getDataScope())) {
						((Map<String, Object>) arg).put("owner", user.getUsername());
					}
				}
			}
		}
		return pjp.proceed(args);
	}

}
